using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;

partial struct LoseTargetSystem : ISystem
{
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        foreach ((RefRO<LocalTransform> localTransform, RefRW<Target> target,RefRW<LoseTarget> loseTarget, RefRW<TargetOverride> targetOverride) 
            in SystemAPI.Query<RefRO<LocalTransform>, RefRW<Target>, RefRW <LoseTarget>, RefRW<TargetOverride>> ())
        {
            if (target.ValueRO.tagertEntity == Entity.Null)
            {
                continue;
            }

            if (targetOverride.ValueRO.tagertEntity != Entity.Null)
            {
                continue;
            }

            LocalTransform targetPostion = SystemAPI.GetComponent<LocalTransform>(target.ValueRO.tagertEntity);
            if (math.distance(localTransform.ValueRO.Position, targetPostion.Position) > loseTarget.ValueRO.loseTargetDistance)
            {
                target.ValueRW.tagertEntity = Entity.Null;
            }
        }
    }

}
